#include <stdio.h>
#include <unistd.h>

#define REGS_FOREACH(_)  _(X) _(Y) _(Z) _(W)    // bit3 bit2 bit1 bit0
#define OUTS_FOREACH(_)  _(A) _(B) _(C) _(D) _(E) _(F) _(G)

//  0   1    2     3    4    5    6    7    8    9
//0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
#define RUN_LOGIC   X1 = (Y & Z & W) ? (!X) : X; \
                    Y1 = (Z & W) ? (!Y): Y; \
                    Z1 = (W) ? (!Z) : Z; \
                    W1 = !W; 

/*
    AAAAA
   F    B
   F    B
   GGGGG
  E    C
  E    C
   DDDDD
*/
// 0000(ABCDEF) 0001(BC) 0010(ABGED) 0011(ABCDG)
// 0100(FGBC) 0101(AFGCD) 0110(AFGCDE) 0111(ABC)
// 1000(ABCDEFG) 1001(ABCDFG) 1010(ABCEFG) 1011(CDEFG)
// 1100(AFED) 1101(BCDEG)  1110(ADEFG) 1111(AEFG)
static const char _A[16] = {1,0,1,1, 0,1,1,1, 1,1,1,0, 1,0,1,1};
static const char _B[16] = {1,1,1,1, 1,0,0,1, 1,1,1,0, 0,1,0,0};
static const char _C[16] = {1,1,0,1, 1,1,1,1, 1,1,1,1, 0,1,0,0};
static const char _D[16] = {1,0,1,1, 0,1,1,0, 1,1,0,1, 1,1,1,0};
static const char _E[16] = {1,0,1,0, 0,0,1,0, 1,0,1,1, 1,1,1,1};
static const char _F[16] = {1,0,0,0, 1,1,1,0, 1,1,1,1, 1,0,1,1};
static const char _G[16] = {0,0,1,1, 1,1,1,0, 1,1,1,1, 0,1,1,1};

#define SEG_LOGIC       A = _A[X<<3 | Y<<2 | Z<<1 | W]; \
                        B = _B[X<<3 | Y<<2 | Z<<1 | W]; \
                        C = _C[X<<3 | Y<<2 | Z<<1 | W]; \
                        D = _D[X<<3 | Y<<2 | Z<<1 | W]; \
                        E = _E[X<<3 | Y<<2 | Z<<1 | W]; \
                        F = _F[X<<3 | Y<<2 | Z<<1 | W]; \
                        G = _G[X<<3 | Y<<2 | Z<<1 | W]; 

#define DEFINE(X)       static int X, X##1;
#define UPDATE(X)       X = X##1;
#define PRINT(X)        printf(#X " = %d; ", X);

int main()
{
    int i=99;
    REGS_FOREACH(DEFINE);
    OUTS_FOREACH(DEFINE);
    while(i--){
        RUN_LOGIC;
        REGS_FOREACH(UPDATE);

        SEG_LOGIC;
        OUTS_FOREACH(PRINT);
        putchar('\n');
        fflush(stdout);
        sleep(1);
    }

    return 0;
}


